#include "gm3d.h"
//我们读入一个界面数据 插值计算每个块体中心位置的值 然后按情况赋值
void GM3D::AddInterfaceBlock(modelist para_list){
	int m, n, xnum, ynum;
	double xs,xe,xmin,xmax,dx;
	double ys,ye,ymin,ymax,dy;
	bool model_added = false;

	string temp_str;
	stringstream temp_ss;

	vertex temp_topo;
	vertexVector face_topo;

	ifstream infile;
	gctl::open_infile(infile, para_list.mod_para);

	while (getline(infile,temp_str))
	{
		//#range必须出现在数据之前
		if (temp_str[0] == '#')
		{
			if (6 == sscanf(temp_str.c_str(),"# range=%lf/%lf/%lf/%lf/%lf/%lf", 
				&xs,&dx,&xe,&ys,&dy,&ye)){
				xmin = GCTL_MIN(xs,xe); xmax = GCTL_MAX(xs,xe);
				ymin = GCTL_MIN(ys,ye); ymax = GCTL_MAX(ys,ye);
				dx = fabs(dx); dy = fabs(dy);
				xnum = round((xmax - xmin)/dx) + 1;
				ynum = round((ymax - ymin)/dy) + 1;
			}
			else continue;
		}
		else{
			gctl::str2ss(temp_str, temp_ss);
			temp_ss >> temp_topo.x >> temp_topo.y >> temp_topo.z;
			face_topo.push_back(temp_topo);
		}
	}
	infile.close();

	if (para_list.val_type == "replace/top")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z >= temp_topo.z){
				model_block_val_[i] = para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "replace/bot")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z <= temp_topo.z){
				model_block_val_[i] = para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "add/top")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z >= temp_topo.z){
				if (model_block_val_[i] == GCTL_BDL_MAX)
					model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				else
					model_block_val_[i] += para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "add/bot")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z <= temp_topo.z){
				if (model_block_val_[i] == GCTL_BDL_MAX)
					model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				else
					model_block_val_[i] += para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "erase/top")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z >= temp_topo.z){
				model_block_val_[i] = GCTL_BDL_MAX;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "erase/bot")
	{
		for (int i = 0; i < model_num_; i++){
			m = floor((model_cube_[i].cen.x - xmin)/dx);
			n = floor((model_cube_[i].cen.y - ymin)/dy);

			temp_topo.z = gctl::rect_interpolate(xmin+dx*m,ymin+dy*n,dx,dy,model_cube_[i].cen.x,model_cube_[i].cen.y,
										face_topo[xnum*n+m].z,face_topo[xnum*n+m+1].z,face_topo[xnum*(n+1)+m].z,face_topo[xnum*(n+1)+m+1].z);

			if (model_cube_[i].cen.z <= temp_topo.z){
				model_block_val_[i] = GCTL_BDL_MAX;
				model_added = true;
			}
		}
	}
	else throw GM3D_INVALID_MODEL_VAL_TYPE;

	if (!model_added)
		throw GM3D_NOMODEL_ADDED;
	return;
}